package com.yrd.centerlog;

import java.io.File;
import java.util.ArrayDeque;
import java.util.Deque;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

public class Utils {
	private static Context context;
	public static void setContext(Context context){
		Utils.context = context;
	}
	public static Context getContext(){
		return context;
	}
	
	public static void printfln(Object... strs){
		System.out.println(StringUtils.join(strs, " "));
	}
	
	public static String standardUnixDirName(String path){
		if(path!=null && path.endsWith("/")){
			path = path.substring(0, path.length()-1);
		}
		return FilenameUtils.separatorsToUnix(path);

	}
	public static  String relativeLocalPath(File baseDir,File file){
		Deque<String> fnameList = new ArrayDeque<String>();
		File tmpfile=file;
		while(tmpfile!=null){
			String tmpfilePath= tmpfile.getAbsolutePath();
			String localBasePath= baseDir.getAbsolutePath();
			if(tmpfilePath.equalsIgnoreCase(localBasePath)){
				break;
			}else{
				fnameList.addFirst(tmpfile.getName());
				tmpfile = tmpfile.getParentFile();
			}
		}
		String rpath = StringUtils.join(fnameList, "/");
		return (rpath==null)? "":rpath ;
	}
}
